package com.quartet.modules.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.quartet.api.PageResult;
import com.quartet.modules.customer.entity.TCustomerInfo;
import com.quartet.modules.customer.service.TCustomerInfoService;
import com.quartet.modules.order.entity.TOrder;
import com.quartet.modules.order.service.TOrderService;
import com.quartet.modules.service.dao.TServiceInfoDao;
import com.quartet.modules.service.dto.TServiceClickCountResponseDto;
import com.quartet.modules.service.dto.TServiceInfoResponseDto;
import com.quartet.modules.service.entity.TServiceInfo;
import com.quartet.modules.service.service.TServiceInfoService;
import com.quartet.modules.statistics.Enum.QueryPeriodEnum;
import com.quartet.modules.statistics.dto.ServiceStatisticsRequestDto;
import com.quartet.modules.statistics.dto.ServiceStatisticsResponseDto;
import com.quartet.modules.system.service.DictDetailServicePs;
import com.quartet.utils.StringUtils;
import com.quartet.utils.constant.MapServerConstant;
import com.quartet.utils.enums.AlarmTypeEnum;
import com.quartet.utils.enums.ServiceTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务信息表(TServiceInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-05-24 17:51:08
 */
@Service("tServiceInfoService")
@Slf4j
public class TServiceInfoServiceImpl extends ServiceImpl<TServiceInfoDao, TServiceInfo> implements TServiceInfoService {

    @Autowired
    private TOrderService tOrderService;

    @Autowired
    private DictDetailServicePs dictDetailServicePs;

    @Autowired
    private TCustomerInfoService tCustomerInfoService;

    /**
     * 功能描述: 给此此订单的此服务自增一次
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/6/11 9:33
     * @param serviceType
     * @param orderCodeList
     * @return void
     */
    @Override
    public void increaseCount(String customerCode,String serviceType, List<String> orderCodeList) {
        this.baseMapper.increaseCount(serviceType, orderCodeList);
    }

    /**
     * 功能描述: 查询服务统计数据
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/7/16 11:00
     * @param
     * @return java.util.List<com.quartet.modules.statistics.dto.ServiceStatisticsResponseDto>
     */
    @Override
    public List<ServiceStatisticsResponseDto> queryServiceStatisticsData(ServiceStatisticsRequestDto serviceStatisticsRequestDto) {
        String queryPeriod = serviceStatisticsRequestDto.getQueryPeriod();
        QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
        queryPeriodEnum.setFilterTime(serviceStatisticsRequestDto);
        List<String> axis = queryPeriodEnum.getAxis();
        List<ServiceStatisticsResponseDto> serviceStatisticsResponseDtoList;
        if (StringUtils.isNotBlank(serviceStatisticsRequestDto.getServiceType())){
            serviceStatisticsRequestDto.setServiceType(ServiceTypeEnum.find(serviceStatisticsRequestDto.getServiceType()).getValue());
        }
        if (QueryPeriodEnum.YESTERDAY.getValue().equals(queryPeriod) || QueryPeriodEnum.TODAY.getValue().equals(queryPeriod)) {
            serviceStatisticsResponseDtoList = this.baseMapper.queryServiceStatisticsDataWithHour(serviceStatisticsRequestDto);
        } else {
            serviceStatisticsResponseDtoList = this.baseMapper.queryServiceStatisticsDataWithDay(serviceStatisticsRequestDto);
        }
        List<ServiceStatisticsResponseDto> list=new ArrayList<>();
        Map<String, ServiceStatisticsResponseDto> map = serviceStatisticsResponseDtoList.stream().collect(Collectors.toMap(ServiceStatisticsResponseDto::getCreateTime, s -> s));
        axis.forEach(x->{
            if (map.get(x)!=null){
                list.add(map.get(x));
            }else {
                ServiceStatisticsResponseDto serviceStatisticsResponseDto=new ServiceStatisticsResponseDto();
                serviceStatisticsResponseDto.setCreateTime(x);
                serviceStatisticsResponseDto.setCount(0);
                list.add(serviceStatisticsResponseDto);
            }
        });

        return list;
    }

    /**
     * 功能描述: 分页查询服务统计信息
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/7/16 13:57
     * @param serviceStatisticsRequestDto
     * @return com.quartet.api.PageResult<com.quartet.modules.statistics.dto.ServiceStatisticsResponseDto>
     */
    @Override
    public PageResult<TServiceClickCountResponseDto> queryServiceStatisticsList(ServiceStatisticsRequestDto serviceStatisticsRequestDto) {
        log.info("---------queryServiceStatisticsList begin, param is {}----------", serviceStatisticsRequestDto);
//        IPage<TServiceInfo> page = new Page<>(serviceStatisticsRequestDto.getCurrent(), serviceStatisticsRequestDto.getLimit());
        String queryPeriod = serviceStatisticsRequestDto.getQueryPeriod();
        QueryPeriodEnum queryPeriodEnum = QueryPeriodEnum.find(queryPeriod);
        queryPeriodEnum.setFilterTime(serviceStatisticsRequestDto);

        com.github.pagehelper.Page<Object> page1 = PageHelper.startPage(serviceStatisticsRequestDto.getCurrent(), serviceStatisticsRequestDto.getLimit());
        List<TServiceClickCountResponseDto> tServiceClickCountResponseDtoList = this.baseMapper.queryServiceStatisticsList(serviceStatisticsRequestDto);
        tServiceClickCountResponseDtoList.forEach(tServiceClickCountResponseDto -> {
            QueryWrapper<TCustomerInfo> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.lambda().eq(TCustomerInfo::getCustomerCode, tServiceClickCountResponseDto.getCustomerCode());
            TCustomerInfo tCustomerInfo = tCustomerInfoService.getOne(queryWrapper4);
            if (!ObjectUtils.isEmpty(tCustomerInfo)) {
                tServiceClickCountResponseDto.setCustomerName(tCustomerInfo.getCustomerName());
            }
            tServiceClickCountResponseDto.setServiceTypeLabel(dictDetailServicePs.getLabelByValueByCode(tServiceClickCountResponseDto.getServiceType()));
        });
        PageResult<TServiceClickCountResponseDto> pageResult = new PageResult();
//        List<String> orderCodeList = null;
//        if (!CollectionUtils.isEmpty(serviceStatisticsRequestDto.getCustomerCodeList())) {
//            QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
//            queryWrapper.lambda().in(TOrder::getCustomerCode, serviceStatisticsRequestDto.getCustomerCodeList());
//            List<TOrder> tOrderList = tOrderService.list(queryWrapper);
//            orderCodeList = tOrderList.stream().map(TOrder::getOrderCode).collect(Collectors.toList());
//            if (CollectionUtils.isEmpty(orderCodeList)) {
//                PageResult<TServiceInfoResponseDto> pageResult = new PageResult();
//                pageResult.setData(Collections.emptyList());
//                pageResult.setTotal(0L);
//            }
//        }
//        QueryWrapper<TServiceInfo> queryWrapper1 = new QueryWrapper<>();
//        if (!CollectionUtils.isEmpty(orderCodeList)) {
//            queryWrapper1.lambda().in(TServiceInfo::getOrderCode, orderCodeList);
//        }
//        if (StringUtils.isNotBlank(serviceStatisticsRequestDto.getServiceName())) {
//            queryWrapper1.lambda().like(TServiceInfo::getServiceName, serviceStatisticsRequestDto.getServiceName());
//        }
//        if (!ObjectUtils.isEmpty(serviceStatisticsRequestDto.getStartTime())) {
//            queryWrapper1.lambda().ge(TServiceInfo::getCreateTime, serviceStatisticsRequestDto.getStartTime());
//        }
//        if (!ObjectUtils.isEmpty(serviceStatisticsRequestDto.getEndTime())) {
//            queryWrapper1.lambda().le(TServiceInfo::getCreateTime, serviceStatisticsRequestDto.getEndTime());
//        }
//        if (StringUtils.isNotBlank(serviceStatisticsRequestDto.getField())) {
//            queryWrapper1.orderBy(true, serviceStatisticsRequestDto.isAsc(), serviceStatisticsRequestDto.getField());
//        } else {
//            queryWrapper1.orderBy(true, serviceStatisticsRequestDto.isAsc(), MapServerConstant.CREATE_TIME);
//        }
//        IPage<TServiceInfo> dbResult = this.baseMapper.selectPage(page, queryWrapper1);
//        PageResult<TServiceInfoResponseDto> pageResult = new PageResult();
//        List<TServiceInfoResponseDto> tServiceInfoResponseDtoList = dbResult.getRecords().stream().map(tServiceInfo -> {
//            TServiceInfoResponseDto tServiceInfoResponseDto = BeanUtil.copyProperties(tServiceInfo, TServiceInfoResponseDto.class);
//            if (!ObjectUtils.isEmpty(tServiceInfo.getAlarmType())) {
//                List<String> alarmTypeList = JSON.parseArray(tServiceInfo.getAlarmType().toString(), String.class);
//                tServiceInfoResponseDto.setAlarmType(alarmTypeList);
//            }
//            QueryWrapper<TOrder> queryWrapper3 = new QueryWrapper<>();
//            queryWrapper3.lambda().eq(TOrder::getOrderCode, tServiceInfo.getOrderCode());
//            tServiceInfoResponseDto.setServiceTypeLabel(dictDetailServicePs.getLabelByValueByCode(tServiceInfoResponseDto.getServiceType()));
//            TOrder tOrder = tOrderService.getOne(queryWrapper3);
//            if (ObjectUtils.isEmpty(tOrder)) {
//                return tServiceInfoResponseDto;
//            }
//            tServiceInfoResponseDto.setOrderName(tOrder.getOrderName());
//
//            QueryWrapper<TCustomerInfo> queryWrapper4 = new QueryWrapper<>();
//            queryWrapper4.lambda().eq(TCustomerInfo::getCustomerCode, tOrder.getCustomerCode());
//            TCustomerInfo tCustomerInfo = tCustomerInfoService.getOne(queryWrapper4);
//            if (ObjectUtils.isEmpty(tCustomerInfo)) {
//                return tServiceInfoResponseDto;
//            }
//            tServiceInfoResponseDto.setCustomerName(tCustomerInfo.getCustomerName());
//            return tServiceInfoResponseDto;
//        }).collect(Collectors.toList());

        pageResult.setTotal(page1.getTotal());
        pageResult.setData(tServiceClickCountResponseDtoList);
        log.info("---------queryServiceStatisticsList end, data is {}----------", pageResult);
        return pageResult;
    }
}